package com.minggo.charmword.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.minggo.charmword.R;
import com.minggo.charmword.adapter.WordStoreAdapter;
import com.minggo.charmword.adapter.WordStoreAdapter.RemarkListener;
import com.minggo.charmword.model.Word;
import com.minggo.charmword.service.CharmWordApplication;

/**
 * 
 * @author minggo
 * @date 2014-9-2 下午5:16:42
 */
public class WordStoreActivity extends Activity implements OnClickListener, RemarkListener {

	private ListView wordStoreLv;
	private WordStoreAdapter storeAdapter;
	private View backV;
	private List<Word> wordList = new ArrayList<Word>();
	private TextView titleTv;
	private String titleStr;

	public static final int NEW_WORD = 1001;
	public static final int REMEMBER_WORD = 1002;

	private int wordType = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_word_store);
		titleStr = getIntent().getStringExtra("title");
		wordType = getIntent().getIntExtra("wordType", 1001);

		initUI();
		requestData();
		CharmWordApplication.allActivities.add(this);
	}

	/**
	 * 请求数据
	 */
	private void requestData() {

		for (int i = 0; i < 10; i++) {
			Word word = new Word();
			word.word = "good" + i;
			word.phonetics = "[gu:d]";
			word.explain = "好的" + i;
			wordList.add(word);
		}
		handler.sendEmptyMessage(1001);
	}

	/**
	 * 刷新UI
	 */
	private void refreshUI() {

		if (storeAdapter == null) {
			if (wordList != null && !wordList.isEmpty()) {
				storeAdapter = new WordStoreAdapter(this, wordList, this, wordType);
				wordStoreLv.setAdapter(storeAdapter);
				AnimationSet set = new AnimationSet(false);
				Animation animation = new AlphaAnimation(0, 1); // AlphaAnimation
																// 控制渐变透明的动画效果
				animation.setDuration(300); // 动画时间毫秒数
				set.addAnimation(animation);
				LayoutAnimationController controller = new LayoutAnimationController(set, 1);
				wordStoreLv.setLayoutAnimation(controller);
			}
		} else {
			storeAdapter.notifyDataSetChanged();
		}
	}

	/**
	 * 初始化UI
	 */
	private void initUI() {
		backV = findViewById(R.id.lo_word_store_back);
		wordStoreLv = (ListView) findViewById(R.id.lv_word_store);
		titleTv = (TextView) findViewById(R.id.tv_word_list);
		if (titleStr != null) {
			titleTv.setText(titleStr);
		}
		backV.setOnClickListener(this);
	}

	private Handler handler = new Handler(new Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case 1001:
				refreshUI();
				break;

			default:
				break;
			}
			return false;
		}
	});

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lo_word_store_back:
			onBackPressed();
			break;

		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	@Override
	public void onRemark(int position) {
		if (wordType == NEW_WORD) {// 添加到掌握
			wordList.remove(position);
		} else if (wordType == REMEMBER_WORD) {// 移出掌握
			wordList.remove(position);
		}
		storeAdapter.notifyDataSetChanged();
	}

	@Override
	protected void onPause() {
		super.onPause();
		StatService.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		StatService.onResume(this);
	}
}
